
const express = require("express");
const bodyParser = require("body-parser");
const userService = require("../../node/service/userService.js");
const { resSuc, resErr } = require('../utils/resMsg.js');

const router = express.Router();

router.get("/", (req, res) => {
    userService.get().then(user => {
        res.json(resSuc(user));
    }).catch(err => {
        res.json(resErr(500, err.message));
    })
})

router.post("*", bodyParser.urlencoded({ extended: false }));
router.post("*", bodyParser.json());

// Update Project
router.post("/update", (req, res) => {
    userService.updateAndSaveUser(req.body).then(() => { 
        res.json(resSuc());
    }).catch(err => {
        res.json(resErr(400, err.message));
    })
})

// 保存数据到本地js文件
router.post("/save", (req, res) => {
    userService.saveUser().then(() => {
        res.json(resSuc())
    }).catch(err => {
        res.json(resErr(400, err.message));
    })
})

module.exports = router;

